package com.stedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.bean.RepairPersonnel;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.service.RepairPersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/admin/repairPersonnel")
public class RepairPersonnelController {
    @Autowired
    private RepairPersonnelService repairPersonnelService;
    //调用连接池里的数据
    @Value("${pageSize}")
    private Integer pageSize;

    @GetMapping
    public RespBean selectByPage(Integer pageNum, String pname) throws SteduException {
        if(pageNum == null) {
            pageNum = 1;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<RepairPersonnel> repairPersonnelList = repairPersonnelService.selectByPname(pname);
        PageInfo<RepairPersonnel> pageInfo = new PageInfo<>(repairPersonnelList);
        return RespBean.ok("查询成功", pageInfo);
    }

    @PostMapping
    public RespBean insert(@RequestBody RepairPersonnel repairPersonnel) throws SteduException {
        //添加
        if (repairPersonnelService.insert(repairPersonnel)) {
            return RespBean.ok("添加成功~~~");
        } else {
            return RespBean.error("添加失败，请重新添加");
        }
    }
    @DeleteMapping("/{pid}")
    public RespBean delete(@PathVariable("pid") Integer pid) throws SteduException {
        if (repairPersonnelService.delete(pid)) {
            return RespBean.ok("删除成功~~~");
        } else {
            return RespBean.error("删除失败，请重新删除");
        }
    }

    @PutMapping
    public RespBean update(@RequestBody RepairPersonnel repairPersonnel) throws SteduException {
        if (repairPersonnelService.update(repairPersonnel)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败，请重新修改");
        }
    }

    @GetMapping("/select/{pid}")
    public RespBean selectByRid(@PathVariable("pid") Integer pid) {
        RepairPersonnel r = repairPersonnelService.selectByPid(pid);
        return RespBean.ok("", r);
    }

}
